import 'package:flutter/material.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_base_app/config/i18n/i18n.dart';

import '../../../config/i18n/i18n_key.dart';
import '../../../config/theme.dart';

void showAlertDialog(
  String text, {
  String? title,
  bool isShowCancel = false,
  bool clickMaskDismiss = true,
  bool? backDismiss,
  Function()? onDismissCall,
  Function(bool)? onResult,
}) {
  //随机生成tag
  String tag = DateTime.now().millisecondsSinceEpoch.toString();
  SmartDialog.show(
    tag: tag,
    clickMaskDismiss: clickMaskDismiss,
    backType: backDismiss == null ? null : (backDismiss == true ? SmartBackType.normal : SmartBackType.block),
    onDismiss: onDismissCall,
    builder: (context) => SizedBox(
      width: AppUtils.width * 0.8,
      child: AlertDialog(
        title: Text(title ?? I18nKey.tips.trx, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
        content: SizedBox(
          width: AppUtils.width * 0.8 - 30,
          child: Text(text, style: theme.textTheme.bodyLarge, textAlign: TextAlign.center),
        ),
        insetPadding: EdgeInsets.zero,
        actionsPadding: EdgeInsets.zero,
        contentPadding: EdgeInsets.only(top: 20, bottom: 20, left: 15, right: 15),
        actions: [
          if (isShowCancel)
            TextButton(
              onPressed: () {
                onResult?.call(false);
                SmartDialog.dismiss(tag: tag);
              },
              child: Text(I18nKey.cancel.trx),
            ),
          TextButton(
            onPressed: () {
              onResult?.call(true);
              SmartDialog.dismiss(tag: tag);
            },
            child: Text(I18nKey.ok.trx),
          ),
        ],
      ),
    ),
  );
}

///提示对话框
class TipsDialog extends BaseDialog {
  final Function(bool)? onResult;
  final Function()? onDismissCall;
  final double? width;
  final double? height;
  final String? title;
  final String text;
  final Widget? iconWidget;
  final bool? backDismiss;

  //是否显示取消按钮
  final bool isShowCancel;

  TipsDialog({
    super.key,
    required this.text,
    this.title,
    this.width,
    this.height,
    this.iconWidget,
    this.isShowCancel = false,
    this.backDismiss,
    this.onResult,
    this.onDismissCall,
  });

  @override
  State createState() => _TipsDialogState();

  @override
  void show() {
    SmartDialog.show(
      tag: tag,
      onDismiss: onDismiss,
      onMask: onMask,
      clickMaskDismiss: clickMaskDismiss,
      backType: backDismiss == null ? null : (backDismiss == true ? SmartBackType.normal : SmartBackType.block),
      builder: (context) => this,
    );
  }

  @override
  void onDismiss() {
    super.onDismiss();
    onDismissCall?.call();
  }
}

class _TipsDialogState extends BaseDialogState<TipsDialog> {
  _TipsDialogState() : super();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseDialogWidget(
      width: widget.width ?? (widget.iconWidget == null ? 300 : 390),
      height: widget.height ?? (widget.iconWidget == null ? 200 : 260),
      title: widget.title ?? I18nKey.tips.trx,
      titleWeight: FontWeight.bold,
      onCloseClick: () {
        widget.onResult?.call(false);
        widget.dismiss();
      },
      actions: [
        if (widget.isShowCancel)
          TextButton(
            onPressed: () {
              widget.onResult?.call(false);
              widget.dismiss();
            },
            child: Text(I18nKey.cancel.trx),
          ),
        TextButton(
          onPressed: () {
            widget.onResult?.call(true);
            widget.dismiss();
          },
          child: Text(I18nKey.ok.trx),
        ),
      ],
      child: _buildContent(),
    );
  }

  Widget _buildContent() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10.0),
      child: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            spacing: 10,
            children: [if (widget.iconWidget != null) widget.iconWidget!, Text(widget.text, style: theme.textTheme.bodyLarge, textAlign: TextAlign.center)],
          ),
        ),
      ),
    );
  }
}
